﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Windows.Forms;
using Tigera.LibCommon;
using Tigera.Network;
using Tigera.T_Definition;
using Tigera.LibCodeUtils;
using System.Xml;

namespace StockMonitor
{
    /// <summary>
    /// AppSettings工具类，可读可写，可监控配置文件变化并自动重载
    /// </summary>
    /// <remarks>
    /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
    /// 编写时间：<datetime>2012-10-16 4:56 PM</datetime>
    /// </remarks>
    public class AppMgr : IDisposable
    {     

        static Dictionary<string, object> settings = new Dictionary<string, object>();

       
        HSMQConnection _mqclient;
        

        public HSMQConnection Mqclient
        {
            get 
            {
                if (_mqclient == null )
                {
                    _mqclient = new HSMQConnection();
                }
                
                return _mqclient; 
            }          
        }
        
        


        static AppMgr instance = new AppMgr();


        static string _traderiskexname;
        public static string TradeRiskExName
        {
            get
            {    
                if (string.IsNullOrEmpty(_traderiskexname))
                {
                    _traderiskexname = GetSetting("configuration", "network", "mqexchange");
                }
                return _traderiskexname;    
            }
        }


        

        static AppMgr()
        {
            try
            {                
                
                _cfg = new XmlFiles();
                _cfg.Load("Plugin.HSSharp.xml");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("无法加载配置文件" + ex.ToString());

            }

            

        }

        static XmlFiles _cfg;
        public static Tigera.LibCommon.XmlFiles Cfg
        {
            get
            {
                if (_cfg == null)
                {
                    try
                    {

                        _cfg = new XmlFiles();
                        _cfg.Load("Plugin.HSSharp.xml");
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("无法加载配置文件" + ex.ToString());

                    }
                }
                return _cfg; 
            }
            //set { _cfg = value; }
        }
        public static string GetSetting(params string[] datas)
        {

            var ret = Cfg.GetNodeValue(datas);
            return ret;
        }

       

        public static AppMgr Instance
        {
            get
            {
                return instance;
            }
        }
      
        
        private bool logined;

        public bool Logined
        {
            get { return logined; }
            set { logined = value; }
        }


       

        #region Public Methods.

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:57 PM</datetime>
        /// </remarks>
        public static T GetValue<T>(string key)
        {
            return GetValue<T>(key, default(T));
        }

        /// <summary>
        /// 获取配置项的值，若为空，则使用给定的默认值
        /// </summary>
        /// <typeparam name="T">值的类型</typeparam>
        /// <param name="key">键.</param>
        /// <param name="defaultValue">若无此配置，则使用的默认值.</param>
        /// <returns>值</returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:57 PM</datetime>
        /// </remarks>
        public static T GetValue<T>(string key, T defaultValue)
        {
            return GetValue<T>(key, default(T), false);
        }

        /// <summary>
        /// 获取配置项的值，并可以顺带将默认值设置进配置文件
        /// </summary>
        /// <typeparam name="T">值的类型</typeparam>
        /// <param name="key">键.</param>
        /// <param name="defaultValue">若无此配置，则使用的默认值.</param>
        /// <param name="SetDefaultValue">如果为 <c>true</c> 则顺带将默认值设置进配置文件.</param>
        /// <returns>值</returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:57 PM</datetime>
        /// </remarks>
        public static T GetValue<T>(string key, T defaultValue, bool SetDefaultValue)
        {
            T value = defaultValue;

            if (settings.ContainsKey(key))
                value = (T)Convert.ChangeType(settings[key], typeof(T));
            else if (SetDefaultValue)
                SetValue(key, defaultValue.ToString());

            return value;
        }

        /// <summary>
        /// 设置配置项的值.
        /// </summary>
        /// <param name="key">键.</param>
        /// <param name="value">待设置的值.</param>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:57 PM</datetime>
        /// </remarks>
        public static void SetValue(string key, object value)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (settings.ContainsKey(key))
                config.AppSettings.Settings.Remove(key);

            config.AppSettings.Settings.Add(key, value.ToString());

            config.Save(ConfigurationSaveMode.Modified);

        }

        public  void Dispose()
        {
            if (this._mqclient != null)
            {
                this._mqclient.Dispose();
            }
        }
        #endregion
    }

   }


